package pers.qianyu.month_202012.date_20201206;

import java.util.*;

/**
 * 860. 柠檬水找零
 * https://leetcode-cn.com/problems/lemonade-change/
 *
 * @author mizzle rain
 * @date 2020-12-06 15:41
 */
public class LemonadeChange {
    /**
     * 暴力模拟
     */
    public boolean lemonadeChange(int[] bills) {
        ArrayList<Integer> list = new ArrayList<>();
        for (int b : bills) {
            if (b == 10) {
                if (list.contains(5)) {
                    int i = list.indexOf(5);
                    list.remove(i);
                } else {
                    return false;
                }
            } else if (b == 20) {
                if (list.contains(5) && list.contains(10)) {
                    int i = list.indexOf(5);
                    list.remove(i);
                    i = list.indexOf(10);
                    list.remove(i);
                } else if (list.contains(5)) {
                    long count = list.stream().filter(x -> x == 5).count();
                    if (count >= 3) {
                        int i = list.indexOf(5);
                        list.remove(i);
                        i = list.indexOf(5);
                        list.remove(i);
                        i = list.indexOf(5);
                        list.remove(i);
                    } else {
                        return false;
                    }
                } else {
                    return false;
                }
            }
            list.add(b);
        }
        return true;
    }

    public boolean lemonadeChange2(int[] bills) {
        int five = 0, ten = 0;
        for (int bill : bills) {
            if (bill == 5) {
                five++;
            } else if (bill == 10) {
                ten++;
                five--;
            } else if (bill == 20) {
                if (ten > 0) {
                    ten--;
                    five--;
                } else {
                    five -= 3;
                }
            }
            if (five < 0) {
                return false;
            }
        }
        return true;
    }
}
